package com.jingan.nursinghome.regulatory.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.text.StaticLayout;
import android.util.AttributeSet;

import cn.android.support.v7.lib.sin.crown.base.BaseView;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/11.
 */

public class iView extends BaseView {
    public iView(Context context) {
        super(context);
        init();
    }

    public iView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    int textSize;

    private void init() {
        textSize = ProportionUtils.getInstance().adapterInt(30);
        textPaint.setTextSize(textSize);
        textPaint.setTextAlign(Paint.Align.LEFT);
    }

    String info;
    String i;


    StaticLayout staticLayout;
    public int LineCount=1;
    public void setInfo(String i, String info) {
        if(info==null){
            return;
        }
        this.info = info.trim();
        this.i = i;
        if(mWidth==0){
            staticLayout=new StaticLayout(info.trim(),textPaint, ProportionUtils.getInstance().adapterInt(588), Layout.Alignment.ALIGN_NORMAL,1.0f,1.0f,true);
        }else {
            staticLayout=new StaticLayout(info.trim(),textPaint, (int) (mWidth*0.88), Layout.Alignment.ALIGN_NORMAL,1.0f,1.0f,true);
        }
        LineCount=staticLayout.getLineCount();
        requestLayout();
    }

    public String getInfo() {
        return info;
    }

    public int height;

    int mWidth=0;

    public void setmWidth(int mWidth) {
        this.mWidth = mWidth;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(info!=null&&staticLayout!=null){
            LineCount=staticLayout.getLineCount();
            height=(int) (textSize * 1.3 * LineCount);
            if(mWidth==0){
                setMeasuredDimension(ProportionUtils.getInstance().adapterInt(650),height);
            }else {
                setMeasuredDimension(mWidth,height);
            }
        }else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        //canvas.drawColor(Color.BLUE);
        if (info != null && !info.trim().equals("")) {
            textPaint.setColor(Color.parseColor("#444444"));
            canvas.drawText(i + "、", 0, textSize * 1.1f, textPaint);
            textPaint.setColor(Color.parseColor("#888888"));
            int x = (int) textPaint.measureText(i + "、", 0, i.length() + 1);

            canvas.save();
            canvas.translate(x,0);
            staticLayout.draw(canvas);
            canvas.restore();//别忘了restore

        }
    }
}
